package com.project.nimsen.common.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import com.project.nimsen.common.FileUpload;
import com.project.nimsen.util.NimsenUtil;
import com.project.nimsen.util.WebUtil;
import com.project.jdbc.utils.ClassUtils;

public class OOSUpload extends FileUpload {
	private File fin = null;
	//oos上传客户端
	OSSClient client;
	private HttpServletRequest request;
	/**
	 * 上传一个文件
	 * @param in 文件信息
	 * @param request
	 * */
	public OOSUpload(File in,  HttpServletRequest request) {
		this.fin = in;
		this.request = request;
		client = NimsenUtil.getOSSClient();
	}
	private String autoFilename;
	public String getAutoFilename() {
		return autoFilename;
	}
	public String getRootPath() {
		return  WebUtil.webUrl(request)+"/file";
	}

	public String upload() throws Exception {
		String srcFileName = fin.getName();
		//上传oos的流信息
		InputStream content = new FileInputStream(fin);
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentLength(fin.length());
		//自定义的文件名称
		autoFilename = ClassUtils.autoUUID()+srcFileName.substring(srcFileName.lastIndexOf("."));
		//上传结果
		PutObjectResult result = client.putObject(NimsenUtil.getOSSBucketName(), autoFilename, content, meta);
		fin.delete();
		return srcFileName;
	}
	
//	public void fileupload() throws Exception{
//		File fin1 = new File("c:/aa.jpg");
//		InputStream content = new FileInputStream(fin1);
//		ObjectMetadata meta = new ObjectMetadata();
//		meta.setContentLength(fin1.length());
//		OSSClient client1 = new OSSClient("accessKeyId", "accessKeySecret");
//		PutObjectResult result = client.putObject("bBucketName", "filename", content, meta);
//		fin1.delete();
//	}

}
